
/*
 * Copyright 2002-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2023原作者。
 *
 *根据Apache许可证2.0版许可（“许可证”）；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本：
 *
 *https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证进行的分发是在“按原样”的基础上进行的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证和
 *许可证下的限制。
 */

package org.springframework.scheduling;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;

import org.springframework.lang.Nullable;

/**
 * Task scheduler interface that abstracts the scheduling of
 * {@link Runnable Runnables} based on different kinds of triggers.
 *
 * <p>This interface is separate from {@link SchedulingTaskExecutor} since it
 * usually represents a different kind of backend, i.e. a thread pool with
 * different characteristics and capabilities. Implementations may implement
 * both interfaces if they can handle both kinds of execution characteristics.
 *
 * <p>The 'default' implementation is
 * {@link org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler},
 * wrapping a native {@link java.util.concurrent.ScheduledExecutorService}
 * and adding extended trigger capabilities.
 *
 * <p>This interface is roughly equivalent to a JSR-236
 * {@code ManagedScheduledExecutorService} as supported in Jakarta EE
 * environments but aligned with Spring's {@code TaskExecutor} model.
 *
 * @author Juergen Hoeller
 * @since 3.0
 * @see org.springframework.core.task.TaskExecutor
 * @see java.util.concurrent.ScheduledExecutorService
 * @see org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
 */
/**
 *任务调度程序接口，用于抽象
 *｛@link Runnable Runnables｝基于不同类型的触发器。
 *
 *<p>此接口与｛@link SchedulingTaskExecutor｝是分开的，因为它
 *通常表示不同类型的后端，即具有
 *不同的特性和能力。实现可能实现
 *如果两个接口都能处理这两种执行特性。
 *
 *<p>“默认”实现是
 *｛@link org.springframework.scheducing.concurrent.ThreadPoolTaskScheduler｝，
 *包装本机｛@link java.util.concurrent.ScheduledExecutiorService｝
 *以及添加扩展的触发功能。
 *
 *<p>这个接口大致相当于JSR-236
 *｛@code ManagedScheduledExecutiorService｝在雅加达EE中得到支持
 *环境，但与Spring的{@code-TaskExecutor}模型一致。
 *
 *@作者于尔根·霍勒
 *@自3.0起
 *@参见org.springframework.core.task.TaskExecutitor
 *@参见java.util.concurrent.ScheduledExecutiorService
 *@请参阅org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
 */
public interface TaskScheduler {

	/**
	 * Return the clock to use for scheduling purposes.
	 * @since 5.3
	 * @see Clock#systemDefaultZone()
	 */
	/**
	 *返回时钟以用于计划目的。
	 *@自5.3起
	 *@参见时钟#systemDefaultZone（）
	 */
	default Clock getClock() {
		return Clock.systemDefaultZone();
	}

	/**
	 * Schedule the given {@link Runnable}, invoking it whenever the trigger
	 * indicates a next execution time.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param trigger an implementation of the {@link Trigger} interface,
	 * e.g. a {@link org.springframework.scheduling.support.CronTrigger} object
	 * wrapping a cron expression
	 * @return a {@link ScheduledFuture} representing pending completion of the task,
	 * or {@code null} if the given Trigger object never fires (i.e. returns
	 * {@code null} from {@link Trigger#nextExecutionTime})
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @see org.springframework.scheduling.support.CronTrigger
	 */
	/**
	 *调度给定的｛@link Runnable｝，每当触发器
	 *指示下一个执行时间。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param触发器｛@link触发器｝接口的实现，
	 *例如一个{@linkorg.springframework.scheduling.support.CronTrigger}对象
	 *包装cron表达式
	 *@return一个{@link ScheduledFuture}，表示任务的待完成，
	 *或者{@code-null}，如果给定的Trigger对象从未激发（即返回
	 *｛@code null｝来自｛@link触发器#nextExecutionTime｝）
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@参见org.springframework.scheduling.support.CronTrigger
	 */
	@Nullable
	ScheduledFuture<?> schedule(Runnable task, Trigger trigger);

	/**
	 * Schedule the given {@link Runnable}, invoking it at the specified execution time.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param startTime the desired execution time for the task
	 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @since 5.0
	 */
	/**
	 *安排给定的｛@link Runnable｝，在指定的执行时间调用它。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param startTime任务所需的执行时间
	 *（如果这是过去的事，任务将立即执行，即尽快执行）
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@自5.0起
	 */
	ScheduledFuture<?> schedule(Runnable task, Instant startTime);

	/**
	 * Schedule the given {@link Runnable}, invoking it at the specified execution time.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param startTime the desired execution time for the task
	 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @deprecated as of 6.0, in favor of {@link #schedule(Runnable, Instant)}
	 */
	/**
	 *安排给定的｛@link Runnable｝，在指定的执行时间调用它。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param startTime任务所需的执行时间
	 *（如果这是过去的事，任务将立即执行，即尽快执行）
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@从6.0起已弃用，支持{@link#schedule（Runnable，Instant）}
	 */
	@Deprecated(since = "6.0")
	default ScheduledFuture<?> schedule(Runnable task, Date startTime) {
		return schedule(task, startTime.toInstant());
	}

	/**
	 * Schedule the given {@link Runnable}, invoking it at the specified execution time
	 * and subsequently with the given period.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param startTime the desired first execution time for the task
	 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
	 * @param period the interval between successive executions of the task
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if  the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @since 5.0
	 */
	/**
	 *安排给定的｛@link Runnable｝，在指定的执行时间调用它
	 *并且随后具有给定的周期。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param startTime任务所需的第一次执行时间
	 *（如果这是过去的事，任务将立即执行，即尽快执行）
	 *@param period任务连续执行之间的间隔
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@自5.0起
	 */
	ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period);

	/**
	 * Schedule the given {@link Runnable}, invoking it at the specified execution time
	 * and subsequently with the given period.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param startTime the desired first execution time for the task
	 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
	 * @param period the interval between successive executions of the task (in milliseconds)
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if  the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @deprecated as of 6.0, in favor of {@link #scheduleAtFixedRate(Runnable, Instant, Duration)}
	 */
	/**
	 *安排给定的｛@link Runnable｝，在指定的执行时间调用它
	 *并且随后具有给定的周期。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param startTime任务所需的第一次执行时间
	 *（如果这是过去的事，任务将立即执行，即尽快执行）
	 *@param period任务连续执行之间的间隔（以毫秒为单位）
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@从6.0起已弃用，支持{@link#scheduleAtFixedRate（Runnable、Instant、Duration）}
	 */
	@Deprecated(since = "6.0")
	default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
		return scheduleAtFixedRate(task, startTime.toInstant(), Duration.ofMillis(period));
	}

	/**
	 * Schedule the given {@link Runnable}, starting as soon as possible and
	 * invoking it with the given period.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param period the interval between successive executions of the task
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @since 5.0
	 */
	/**
	 *安排给定的｛@link Runnable｝，尽快开始并
	 *以给定的周期调用它。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param period任务连续执行之间的间隔
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@自5.0起
	 */
	ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period);

	/**
	 * Schedule the given {@link Runnable}, starting as soon as possible and
	 * invoking it with the given period.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param period the interval between successive executions of the task (in milliseconds)
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @deprecated as of 6.0, in favor of {@link #scheduleAtFixedRate(Runnable, Duration)}
	 */
	/**
	 *安排给定的｛@link Runnable｝，尽快开始并
	 *以给定的周期调用它。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param period任务连续执行之间的间隔（以毫秒为单位）
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@从6.0起已弃用，支持{@link#scheduleAtFixedRate（Runnable，Duration）}
	 */
	@Deprecated(since = "6.0")
	default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
		return scheduleAtFixedRate(task, Duration.ofMillis(period));
	}

	/**
	 * Schedule the given {@link Runnable}, invoking it at the specified execution time
	 * and subsequently with the given delay between the completion of one execution
	 * and the start of the next.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param startTime the desired first execution time for the task
	 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
	 * @param delay the delay between the completion of one execution and the start of the next
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @since 5.0
	 */
	/**
	 *安排给定的｛@link Runnable｝，在指定的执行时间调用它
	 *然后在一次执行完成之间的给定延迟
	 *以及下一个的开始。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param startTime任务所需的第一次执行时间
	 *（如果这是过去的事，任务将立即执行，即尽快执行）
	 *@param delay一次执行完成与下一次执行开始之间的延迟
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@自5.0起
	 */
	ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay);

	/**
	 * Schedule the given {@link Runnable}, invoking it at the specified execution time
	 * and subsequently with the given delay between the completion of one execution
	 * and the start of the next.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param startTime the desired first execution time for the task
	 * (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
	 * @param delay the delay between the completion of one execution and the start of the next
	 * (in milliseconds)
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @deprecated as of 6.0, in favor of {@link #scheduleWithFixedDelay(Runnable, Instant, Duration)}
	 */
	/**
	 *安排给定的｛@link Runnable｝，在指定的执行时间调用它
	 *然后在一次执行完成之间的给定延迟
	 *以及下一个的开始。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param startTime任务所需的第一次执行时间
	 *（如果这是过去的事，任务将立即执行，即尽快执行）
	 *@param delay一次执行完成与下一次执行开始之间的延迟
	 *（以毫秒为单位）
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@从6.0起已弃用，支持{@link#scheduleWithFixedDelay（Runnable，Instant，Duration）}
	 */
	@Deprecated(since = "6.0")
	default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
		return scheduleWithFixedDelay(task, startTime.toInstant(), Duration.ofMillis(delay));
	}

	/**
	 * Schedule the given {@link Runnable}, starting as soon as possible and invoking it with
	 * the given delay between the completion of one execution and the start of the next.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param delay the delay between the completion of one execution and the start of the next
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @since 5.0
	 */
	/**
	 *安排给定的｛@link Runnable｝，尽快启动并用
	 *一次执行完成与下一次执行开始之间的给定延迟。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param delay一次执行完成与下一次执行开始之间的延迟
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@自5.0起
	 */
	ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay);

	/**
	 * Schedule the given {@link Runnable}, starting as soon as possible and invoking it with
	 * the given delay between the completion of one execution and the start of the next.
	 * <p>Execution will end once the scheduler shuts down or the returned
	 * {@link ScheduledFuture} gets cancelled.
	 * @param task the Runnable to execute whenever the trigger fires
	 * @param delay the delay between the completion of one execution and the start of the next
	 * (in milliseconds)
	 * @return a {@link ScheduledFuture} representing pending completion of the task
	 * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
	 * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
	 * @deprecated as of 6.0, in favor of {@link #scheduleWithFixedDelay(Runnable, Duration)}
	 */
	/**
	 *安排给定的｛@link Runnable｝，尽快启动并用
	 *一次执行完成与下一次执行开始之间的给定延迟。
	 *<p>一旦调度程序关闭或返回
	 *｛@link ScheduledFuture｝被取消。
	 *@param task可在触发器触发时执行的Runnable
	 *@param delay一次执行完成与下一次执行开始之间的延迟
	 *（以毫秒为单位）
	 *@return一个｛@link ScheduledFuture｝，表示任务的挂起完成
	 *@throws org.springframework.core.task.TaskRejectedException如果给定任务未被接受
	 *由于内部原因（例如，池过载处理策略或正在关闭池）
	 *@从6.0起已弃用，支持{@link#scheduleWithFixedDelay（Runnable，Duration）}
	 */
	@Deprecated(since = "6.0")
	default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
		return scheduleWithFixedDelay(task, Duration.ofMillis(delay));
	}

}
